home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / show / mpeg2decodeWOS.lha / mpeg2decode / src / motion.c < prev    next >
C/C++ Source or Header  |  1999-02-23  |  8KB  |  255 lines

  1. /* motion.c, motion vector decoding                                         */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. /* private prototypes */
  36. static void decode_motion_vector _ANSI_ARGS_((int *pred, int r_size, int motion_code,
  37.   int motion_residualesidual, int full_pel_vector));
  38.  
  39. /* ISO/IEC 13818-2 sections 6.2.5.2, 6.3.17.2, and 7.6.3: Motion vectors */
  40. #ifdef STORM
  41. void motion_vectors(int PMV[2][2][2],int dmvector[2],
  42.   int motion_vertical_field_select[2][2],int s,int motion_vector_count,int mv_format,int h_r_size,int v_r_size,int dmv,int mvscale)
  43. #else
  44. void motion_vectors(PMV,dmvector,
  45.   motion_vertical_field_select,s,motion_vector_count,mv_format,h_r_size,v_r_size,dmv,mvscale)
  46. int PMV[2][2][2];
  47. int dmvector[2];
  48. int motion_vertical_field_select[2][2];
  49. int s, motion_vector_count, mv_format, h_r_size, v_r_size, dmv, mvscale;
  50. #endif
  51. {
  52.   if (motion_vector_count==1)
  53.   {
  54.     if (mv_format==MV_FIELD && !dmv)
  55.     {
  56.       motion_vertical_field_select[1][s] = motion_vertical_field_select[0][s] = Get_Bits(1);
  57. #ifdef TRACE
  58.       if (Trace_Flag)
  59.       {
  60.         printf("motion_vertical_field_select[][%d] (%d): %d\n",s,
  61.           motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
  62.       }
  63. #endif /* TRACE */
  64.     }
  65.  
  66.     motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  67.  
  68.     /* update other motion vector predictors */
  69.     PMV[1][s][0] = PMV[0][s][0];
  70.     PMV[1][s][1] = PMV[0][s][1];
  71.   }
  72.   else
  73.   {
  74.     motion_vertical_field_select[0][s] = Get_Bits(1);
  75. #ifdef TRACE
  76.     if (Trace_Flag)
  77.     {
  78.       printf("motion_vertical_field_select[0][%d] (%d): %d\n",s,
  79.         motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
  80.     }
  81. #endif /* TRACE */
  82.     motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  83.  
  84.     motion_vertical_field_select[1][s] = Get_Bits(1);
  85. #ifdef TRACE
  86.     if (Trace_Flag)
  87.     {
  88.       printf("motion_vertical_field_select[1][%d] (%d): %d\n",s,
  89.         motion_vertical_field_select[1][s],motion_vertical_field_select[1][s]);
  90.     }
  91. #endif /* TRACE */
  92.     motion_vector(PMV[1][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
  93.   }
  94. }
  95.  
  96. /* get and decode motion vector and differential motion vector 
  97.    for one prediction */
  98. #ifdef STORM
  99. void motion_vector(int *PMV,int *dmvector,
  100.   int h_r_size,int v_r_size,int dmv,int mvscale,int full_pel_vector)
  101. #else
  102. void motion_vector(PMV,dmvector,
  103.   h_r_size,v_r_size,dmv,mvscale,full_pel_vector)
  104. int *PMV;
  105. int *dmvector;
  106. int h_r_size;
  107. int v_r_size;
  108. int dmv; /* MPEG-2 only: get differential motion vectors */
  109. int mvscale; /* MPEG-2 only: field vector in frame pic */
  110. int full_pel_vector; /* MPEG-1 only */
  111. #endif
  112. {
  113.   int motion_code, motion_residual;
  114.  
  115.   /* horizontal component */
  116.   /* ISO/IEC 13818-2 Table B-10 */
  117.   motion_code = Get_motion_code();
  118.  
  119.   motion_residual = (h_r_size!=0 && motion_code!=0) ? Get_Bits(h_r_size) : 0;
  120.  
  121. #ifdef TRACE
  122.   if (Trace_Flag)
  123.   {
  124.     if (h_r_size!=0 && motion_code!=0)
  125.     {
  126.       printf("motion_residual (");
  127.       Print_Bits(motion_residual,h_r_size,h_r_size);
  128.       printf("): %d\n",motion_residual);
  129.     }
  130.   }
  131. #endif /* TRACE */
  132.  
  133.  
  134.   decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);
  135.  
  136.   if (dmv)
  137.     dmvector[0] = Get_dmvector();
  138.  
  139.  
  140.   /* vertical component */
  141.   motion_code     = Get_motion_code();
  142.   motion_residual = (v_r_size!=0 && motion_code!=0) ? Get_Bits(v_r_size) : 0;
  143.  
  144. #ifdef TRACE
  145.   if (Trace_Flag)
  146.   {
  147.     if (v_r_size!=0 && motion_code!=0)
  148.     {
  149.       printf("motion_residual (");
  150.       Print_Bits(motion_residual,v_r_size,v_r_size);
  151.       printf("): %d\n",motion_residual);
  152.     }
  153.   }
  154. #endif /* TRACE */
  155.  
  156.   if (mvscale)
  157.     PMV[1] >>= 1; /* DIV 2 */
  158.  
  159.   decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);
  160.  
  161.   if (mvscale)
  162.     PMV[1] <<= 1;
  163.  
  164.   if (dmv)
  165.     dmvector[1] = Get_dmvector();
  166.  
  167. #ifdef TRACE
  168.   if (Trace_Flag)
  169.     printf("PMV = %d,%d\n",PMV[0],PMV[1]);
  170. #endif /* TRACE */
  171. }
  172.  
  173. /* calculate motion vector component */
  174. /* ISO/IEC 13818-2 section 7.6.3.1: Decoding the motion vectors */
  175. /* Note: the arithmetic here is more elegant than that which is shown 
  176.    in 7.6.3.1.  The end results (PMV[][][]) should, however, be the same.  */
  177.  
  178. #ifdef STORM
  179. static void decode_motion_vector(int *pred,int r_size,int motion_code,int motion_residual,int full_pel_vector)
  180. #else
  181. static void decode_motion_vector(pred,r_size,motion_code,motion_residual,full_pel_vector)
  182. int *pred;
  183. int r_size, motion_code, motion_residual;
  184. int full_pel_vector; /* MPEG-1 (ISO/IEC 11172-1) support */
  185. #endif
  186. {
  187.   int lim, vec;
  188.  
  189.   lim = 16<<r_size;
  190.   vec = full_pel_vector ? (*pred >> 1) : (*pred);
  191.  
  192.   if (motion_code>0)
  193.   {
  194.     vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
  195.     if (vec>=lim)
  196.       vec-= lim + lim;
  197.   }
  198.   else if (motion_code<0)
  199.   {
  200.     vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
  201.     if (vec<-lim)
  202.       vec+= lim + lim;
  203.   }
  204.   *pred = full_pel_vector ? (vec<<1) : vec;
  205. }
  206.  
  207.  
  208. /* ISO/IEC 13818-2 section 7.6.3.6: Dual prime additional arithmetic */
  209. #ifdef STORM
  210. void Dual_Prime_Arithmetic(int DMV[][2],int *dmvector,int mvx,int mvy)
  211. #else
  212. void Dual_Prime_Arithmetic(DMV,dmvector,mvx,mvy)
  213. int DMV[][2];
  214. int *dmvector; /* differential motion vector */
  215. int mvx, mvy;  /* decoded mv components (always in field format) */
  216. #endif
  217. {
  218.   if (picture_structure==FRAME_PICTURE)
  219.   {
  220.     if (top_field_first)
  221.     {
  222.       /* vector for prediction of top field from bottom field */
  223.       DMV[0][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
  224.       DMV[0][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] - 1;
  225.  
  226.       /* vector for prediction of bottom field from top field */
  227.       DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
  228.       DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
  229.     }
  230.     else
  231.     {
  232.       /* vector for prediction of top field from bottom field */
  233.       DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
  234.       DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
  235.  
  236.       /* vector for prediction of bottom field from top field */
  237.       DMV[1][0] = ((mvx  +(mvx>0))>>1) + dmvector[0];
  238.       DMV[1][1] = ((mvy  +(mvy>0))>>1) + dmvector[1] + 1;
  239.     }
  240.   }
  241.   else
  242.   {
  243.     /* vector for prediction from field of opposite 'parity' */
  244.     DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
  245.     DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
  246.  
  247.     /* correct for vertical field shift */
  248.     if (picture_structure==TOP_FIELD)
  249.       DMV[0][1]--;
  250.     else
  251.       DMV[0][1]++;
  252.   }
  253. }
  254.  
  255.